package com.buanalintas.sandals.pos.purchaseorderdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosMasterSize;
import com.buanalintas.sandals.pos.entities.PosTransactionPurchaseOrderDetail;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PurchaseOrderDetail extends Page {

    private Field purchaseOrderDetailId;
    private Field purchaseOrderDetailPurchaseOrderId;
    private Field purchaseOrderDetailItemId;
    private Field purchaseOrderDetailQuantity;
    private Field purchaseOrderDetailPrice;
    private Field purchaseOrderDetailSizeId;
    private Field purchaseOrderDetailSize;
    private Field purchaseOrderDetailDescription;

    public PurchaseOrderDetail() {
        super();
        setTheClass(PosTransactionPurchaseOrderDetail.class);

        purchaseOrderDetailId = new Field();
        purchaseOrderDetailId.setName("purchaseOrderDetailId");
        purchaseOrderDetailId.setCaption("id");
        purchaseOrderDetailId.setListProperties("");
        purchaseOrderDetailId.setInputProperties("");
        getFields().add(purchaseOrderDetailId);

        purchaseOrderDetailPurchaseOrderId = new Field();
        purchaseOrderDetailPurchaseOrderId.setName("purchaseOrderDetailPurchaseOrderId");
        purchaseOrderDetailPurchaseOrderId.setCaption("purchase");
        purchaseOrderDetailPurchaseOrderId.setListProperties("");
        purchaseOrderDetailPurchaseOrderId.setInputProperties("");
        getFields().add(purchaseOrderDetailPurchaseOrderId);

        purchaseOrderDetailItemId = new Field();
        purchaseOrderDetailItemId.setName("purchaseOrderDetailItemId");
        purchaseOrderDetailItemId.setCaption("item");
        purchaseOrderDetailItemId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        purchaseOrderDetailItemId.setInputProperties(" size=\"30\"");
        //purchaseOrderDetailItemId.setSelectorCondition("t.itemSupplierModelId.supplierModelSupplierId.supplierId= ");
        purchaseOrderDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"5%\" nowrap"});
        purchaseOrderDetailItemId.getSelectorColumns().add(new String[]{"itemModel", " Model", "width=\"45%\""});
        purchaseOrderDetailItemId.getSelectorColumns().add(new String[]{"itemColorId", " Color", "width=\"45%\""});
        purchaseOrderDetailItemId.setSelectorOrder("t.itemCode");
        purchaseOrderDetailItemId.setRequired(true);
        getFields().add(purchaseOrderDetailItemId);

        purchaseOrderDetailSize = new Field();
        purchaseOrderDetailSize.setName("purchaseOrderDetailSize");
        purchaseOrderDetailSize.setCaption("size");
        purchaseOrderDetailSize.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        //purchaseOrderDetailSize.setInputProperties(" size=\"30\"");
        purchaseOrderDetailSize.setEntityRelation(PosMasterSize.class);
        purchaseOrderDetailSize.setInputType(Field.INPUT_CHECK_BOX);
        purchaseOrderDetailSize.setRequired(true);
        getFields().add(purchaseOrderDetailSize);

        purchaseOrderDetailSizeId = new Field();
        purchaseOrderDetailSizeId.setName("purchaseOrderDetailSizeId");
        purchaseOrderDetailSizeId.setCaption("size");
        purchaseOrderDetailSizeId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        //purchaseOrderDetailSizeId.setInputProperties(" size=\"30\"");
        purchaseOrderDetailSizeId.setEntityRelation(PosMasterSize.class);
        //purchaseOrderDetailSizeId.setInputType(Field.INPUT_CHECK_BOX);
        purchaseOrderDetailSizeId.setRequired(true);
        getFields().add(purchaseOrderDetailSizeId);

        purchaseOrderDetailQuantity = new Field();
        purchaseOrderDetailQuantity.setName("purchaseOrderDetailQuantity");
        purchaseOrderDetailQuantity.setCaption("quantity");
        purchaseOrderDetailQuantity.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        purchaseOrderDetailQuantity.setInputProperties(" size=\"3\"");
        purchaseOrderDetailQuantity.setRequired(true);
        getFields().add(purchaseOrderDetailQuantity);

        purchaseOrderDetailPrice = new Field();
        purchaseOrderDetailPrice.setName("purchaseOrderDetailPrice");
        purchaseOrderDetailPrice.setCaption("price");
        purchaseOrderDetailPrice.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        purchaseOrderDetailPrice.setInputProperties(" size=\"8\"");
        purchaseOrderDetailPrice.setRequired(true);
        purchaseOrderDetailPrice.setFormater(MoneyFormatter.getInstance());
        getFields().add(purchaseOrderDetailPrice);

        purchaseOrderDetailDescription = new Field();
        purchaseOrderDetailDescription.setName("purchaseOrderDetailDescription");
        purchaseOrderDetailDescription.setCaption("description");
        purchaseOrderDetailDescription.setListProperties(" width=\"60%\"");
        purchaseOrderDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        purchaseOrderDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        purchaseOrderDetailDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(purchaseOrderDetailDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(purchaseOrderDetailItemId);
        getFieldsAdd().add(purchaseOrderDetailSize);
        getFieldsAdd().add(purchaseOrderDetailQuantity);
        getFieldsAdd().add(purchaseOrderDetailPrice);
        getFieldsAdd().add(purchaseOrderDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(purchaseOrderDetailItemId);
        getFieldsEdit().add(purchaseOrderDetailSizeId);
        getFieldsEdit().add(purchaseOrderDetailQuantity);
        getFieldsEdit().add(purchaseOrderDetailPrice);
        getFieldsEdit().add(purchaseOrderDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(purchaseOrderDetailItemId);
        getFieldsList().add(purchaseOrderDetailSizeId);
        getFieldsList().add(purchaseOrderDetailDescription);
        getFieldsList().add(purchaseOrderDetailQuantity);
        getFieldsList().add(purchaseOrderDetailPrice);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }

    @Override
    public StringBuffer process() {
        if (getAction().equals(Action.TRANS_DETAIL_ADD)) {
            return Add.process(this);
        }
        if (getAction().equals(Action.TRANS_DETAIL_LIST)) {
            return ListTransaction.process(this);
        }
        return super.process();
    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        String supplier = Utility.getParameter(req, "supplier");
        if (supplier != null && !"".equals(supplier)) {
            purchaseOrderDetailItemId.setSelectorCondition("t.itemSupplierModelId.supplierModelSupplierId.supplierId='" + supplier + "'");
        }
    }
}